package com.black.quick.controller;

import com.black.quick.domain.OrderInfo;
import com.black.quick.domain.QuickOrder;
import com.black.quick.domain.QuickUser;
import com.black.quick.redis.RedisService;
import com.black.quick.result.CodeMsg;
import com.black.quick.service.GoodsService;
import com.black.quick.service.OrderService;
import com.black.quick.service.QuickService;
import com.black.quick.service.QuickUserService;
import com.black.quick.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author shy_black
 * @date 2019/7/27 13:20
 * @Description:
 */
@Controller
@RequestMapping("/quick")
public class QuickController {
    @Autowired
    QuickUserService userService;

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    QuickService quickService;

    @RequestMapping("do_quick")
    public String list(Model model, QuickUser user,
                       @RequestParam("goodsId")long goodsId) {
        model.addAttribute("user",user);
        if (user == null) {
            return "login";
        }
        //判断库存
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        int stock = goods.getStockCount();
        if(stock <= 0) {
            model.addAttribute("errmsg", CodeMsg.MIAO_SHA_OVER);
            return "miaosha_fail";
        }
        //判断是否重复下单
        //if(orderService.getQuickOrderByUserIdGoodsId(user.getId(),goodsId) != null) {
        //QuickOrder order = orderService.getQuickOrderByUserIdGoodsId(user.getId(),goodsId);
        //if(order != null) {
//        if( !StringUtils.isEmpty(orderService.getQuickOrderByUserIdGoodsId(user.getId(),goodsId)) )  {
//            model.addAttribute("errmsg",CodeMsg.REPEATE_MIAOSHA);
//            return "miaosha_fail";
//        }
        //减库存，下订单写入秒杀订单
        OrderInfo orderInfo = quickService.quick(user,goods);
        model.addAttribute("orderInfo",orderInfo);
        model.addAttribute("goods",goods);
        return "order_detail";
    }
}
